package com.company.dcm.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.dcm.entity.Dev;
import com.company.dcm.entity.User;
import com.company.dcm.entity.WorkShop;
import com.company.dcm.mapper.DevMapper;
import com.company.dcm.mapper.WorkShopMapper;
import com.company.dcm.service.DevService;
import com.company.dcm.service.PermissionService;
import com.company.dcm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DevServiceImpl extends ServiceImpl<DevMapper, Dev> implements DevService {
    @Autowired
    private DevMapper devMapper;
    @Autowired
    private WorkShopMapper workShopMapper;
//    @Autowired
//    private UserService userService;
//    @Autowired
//    private PermissionService permissionService;
    @Override
    public List<String> getDeviceCodeByAuth() {
        List<String> workShopIds = StpUtil.getPermissionList();
        List<String> result = new ArrayList<>();
        for (String id:workShopIds) {
            List<Dev> deviceCodes = devMapper.selectList(new LambdaQueryWrapper<Dev>().eq(Dev::getId,id));
            for (Dev deviceCode:deviceCodes) {
                result.add(deviceCode.getDeviceCode());
            }
        }
        return result;
    }

    @Override
    public List<String> getDeviceCodeByAuth(String userName) {
//        User user = userService.getExistByUserName(userName);
//
//        List<String> workShopIds = permissionService.getWorkShopIdsByUserId(user.getUserId()).stream()
//                .map(String::valueOf)
//                .collect(Collectors.toList());
        List<String> workShopIds =StpUtil.getPermissionList(userName);
        List<String> result = new ArrayList<>();
        for (String id:workShopIds) {
            List<Dev> deviceCodes = devMapper.selectList(new LambdaQueryWrapper<Dev>().eq(Dev::getId,id));
            for (Dev deviceCode:deviceCodes) {
                result.add(deviceCode.getDeviceCode());
            }
        }


        return result;
    }

    @Override
    public List<String> getDeviceCodeByWorkShopId(Long id) {

        List<String> result = new ArrayList<>();

        List<Dev> deviceCodes = devMapper.selectList(new LambdaQueryWrapper<Dev>().eq(Dev::getId, id));
        for (Dev deviceCode : deviceCodes) {
            result.add(deviceCode.getDeviceCode());
        }

        return result;
    }

    @Override
    public List<String> getDeviceCodeByFactoryId(Long factoryId) {
        List<WorkShop> workShopIds = workShopMapper.selectList(new LambdaQueryWrapper<WorkShop>().eq(WorkShop::getFactoryId,factoryId));
        List<String> result = new ArrayList<>();
        for (WorkShop workShop:workShopIds) {
            List<Dev> deviceCodes = devMapper.selectList(new LambdaQueryWrapper<Dev>().eq(Dev::getId,workShop.getId()));
            for (Dev deviceCode:deviceCodes) {
                result.add(deviceCode.getDeviceCode());
            }
        }
        return result;
    }
}
